using Aurora.AI.Wiki.Services.ChatApplication;
using Aurora.AI.Wiki.Services.ChatApplication.Dialog;
using Aurora.AI.Wiki.Services.ChatApplication.Record;
using Gateway.IApplication.UploadFiles;
using Gateway.IApplication.UploadFiles.Output;
using Gateway.Infrastructure.Provider.Operator;
using Gateway.IQuery;

namespace Gateway.UI.Api.Controllers.Ai.Wiki;

/// <summary>
/// ai 应用 控制器
/// </summary>
[ApiController]
[Route("api/ai/wiki/application")]
public class AiApplicationController(ChatApplicationService chatApplicationService, IOperatorProvider operatorProvider,
    IUploadFilesApp uploadFilesApp) : BaseController
{
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task<bool> Add(ChatApplicationAddRequest request)
    {
        request.OperatorId = operatorProvider.GetOperator().OperatorId;
        return await chatApplicationService.Add(request);
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<bool> Update(ChatApplicationUpdateRequest request)
    {
        return await chatApplicationService.Update(request);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("delete")]
    public async Task<bool> Delete(string id)
    {
        await chatApplicationService.Delete(id);
        return true;
    }

    /// <summary>
    /// 列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("page/list")]
    public async Task<PageResult<ChatApplicationPageListResponse>> GetPageList(BasePageRequest request)
    {
        var data = await chatApplicationService.List(request.Page, request.PageSize);
        return PageResult<ChatApplicationPageListResponse>.Create(data.list, data.total);
    }
    
    /// <summary>
    /// 详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("detail")]
    public async Task<ChatApplicationDetailResponse> Detail(string id)
    {
        return await chatApplicationService.Detail(id);
    }

    /// <summary>
    ///  创建会话
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("chat/dialog/create")]
    public async Task<string> CreateChatDialog(ChatApplicationDialogAddRequest request)
    {
        request.OperatorId = operatorProvider.GetOperator().OperatorId;
        if (string.IsNullOrEmpty(request.AiType))
        {
            request.AiType = "chat";
        }

        return await chatApplicationService.CreateChatDialog(request);
    }

    /// <summary>
    ///  更新会话
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("chat/dialog/update")]
    public async Task<bool> UpdateChatDialog(ChatApplicationDialogUpdateRequest request)
    {
        request.OperatorId = operatorProvider.GetOperator().OperatorId;
        return await chatApplicationService.UpdateChatDialog(request);
    }

    /// <summary>
    ///  删除会话
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("chat/dialog/remove/{id}")]
    public async Task<bool> RemoveChatDialog([FromRoute] string id)
    {
        return await chatApplicationService.RemoveChatDialogAsync(id);
    }

    /// <summary>
    ///  会话列表
    /// </summary>
    /// <param name="applicationId"></param>
    /// <param name="aiType"></param>
    /// <returns></returns>
    [HttpGet("chat/dialog/list")]
    public async Task<List<ChatApplicationDialogListResponse>> ChatDialogList(string applicationId, string aiType)
    {
        if (string.IsNullOrEmpty(aiType))
        {
            aiType = "chat";
        }

        return await chatApplicationService.GetChatDialogListAsync(applicationId, aiType);
    }

    /// <summary>
    ///  聊天记录列表
    /// </summary>
    /// <param name="dialogId"></param>
    /// <returns></returns>
    [HttpGet("chat/record/list")]
    public async Task<List<ChatRecordListResponse>> ChatRecordList(string dialogId)
    {
        return await chatApplicationService.GetRecordListByDialogId(dialogId);
    }

    /// <summary>
    /// 聊天记录创建
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("chat/record/create")]
    public async Task<bool> ChatRecord(ChatRecordCreateRequest request)
    {
        // 在这里需要转存图片
        if (request.Images?.Count > 0)
        {
            List<string> resultImages = new();
            // 需要上传到文件服务中
            foreach (string item in request.Images)
            {
                FileOutput uploadFile = await uploadFilesApp.UploadFile(item);
                if (!string.IsNullOrEmpty(uploadFile.Url))
                {
                    resultImages.Add(uploadFile.Url);
                }
            }

            request.Images = resultImages;
        }

        return await chatApplicationService.AddRecord(request);
    }
}